﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;

namespace StudentWeb.Api.Controllers
{
    public class TouristRegistrationController : ApiController
    {
        TouristRegistrationDataBO _TouristRegistrationDataBO = new TouristRegistrationDataBO();
        #region 游客信息 登录 
        [HttpPost]
        public IHttpActionResult Login([FromBody]JObject obj)
        {
            dynamic model = obj;
            string TouristName = model.TouristName ?? "";
            string TouristPassword = model.TouristPassword ?? "";
            if (string.IsNullOrEmpty(TouristName) || string.IsNullOrEmpty(TouristPassword))
            {
                return Json(new Result(ResultCode.Fail, "用户名或密码不能为空"));
            }
            try
            {
                var result = _TouristRegistrationDataBO.Login(TouristName, TouristPassword);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 游客信息 注册 
        [HttpPost]
        public IHttpActionResult SaveTouristInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            string TouristName = model.TouristName ?? "";
            string TouristPassword = model.TouristPassword ?? "";
            string TouristPhone = model.TouristPhone ?? "";
            string RegistrationDate = model.RegistrationDate ?? "";
            string Gender = model.Gender ?? "";
            string BirthDate = model.BirthDate ?? "";
            if (string.IsNullOrEmpty(TouristName))
            {
                return Json(new Result(ResultCode.Fail, "用户名不能为空"));
            }
            if (string.IsNullOrEmpty(TouristPassword))
            {
                return Json(new Result(ResultCode.Fail, "密码不能为空"));
            }
            if (string.IsNullOrEmpty(TouristPhone))
            {
                return Json(new Result(ResultCode.Fail, "电话不能为空"));
            }
            if (string.IsNullOrEmpty(RegistrationDate))
            {
                return Json(new Result(ResultCode.Fail, "注册日期不能为空"));
            }
            if (string.IsNullOrEmpty(Gender))
            {
                return Json(new Result(ResultCode.Fail, "性别不能为空"));
            }
            if (string.IsNullOrEmpty(BirthDate))
            {
                return Json(new Result(ResultCode.Fail, "出生日期不能为空"));
            }
            try
            {
                var result = _TouristRegistrationDataBO.SaveTouristInfo(model);
                return result ? Json(new Result(ResultCode.Success, "注册成功")) :
               Json(new Result(ResultCode.Fail, "注册失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 游客信息 全部记录
        [HttpGet]
        public IHttpActionResult GetTouristList()
        {
            try
            {
                var result = new TouristRegistrationDataBO().GetTouristList();
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 游客信息 单条记录
        [HttpPost]
        public IHttpActionResult GetTouristRegistrationInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            int TouristID = model.TouristID ?? 0;
            if (TouristID == 0)
            {
                return Json(new Result(ResultCode.Fail, "ID 不能为空"));
            }
            try
            {
                var result = _TouristRegistrationDataBO.GetTouristRegistrationInfo(TouristID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 游客信息 添加
        [HttpPost]
        public IHttpActionResult InsertTouristInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            string TouristName = model.TouristName ?? "";
            string TouristPassword = model.TouristPassword ?? "";
            string TouristPhone = model.TouristPhone ?? "";
            string RegistrationDate = model.RegistrationDate ?? "";
            string Gender = model.Gender ?? "";
            string BirthDate = model.BirthDate ?? "";
            if (string.IsNullOrEmpty(TouristName))
            {
                return Json(new Result(ResultCode.Fail, "游客用户名不能为空"));
            }
            if (string.IsNullOrEmpty(TouristPassword))
            {
                return Json(new Result(ResultCode.Fail, "游客密码不能为空"));
            }
            if (string.IsNullOrEmpty(TouristPhone))
            {
                return Json(new Result(ResultCode.Fail, "电话号码不能为空"));
            }
            if (string.IsNullOrEmpty(RegistrationDate))
            {
                return Json(new Result(ResultCode.Fail, "注册日期不能为空"));
            }
            if (string.IsNullOrEmpty(Gender))
            {
                return Json(new Result(ResultCode.Fail, "性别不能为空"));
            }
            if (string.IsNullOrEmpty(BirthDate))
            {
                return Json(new Result(ResultCode.Fail, "出生日期不能为空"));
            }
            try
            {
                var result = _TouristRegistrationDataBO.InsertTouristInfo(model);
                return result ? Json(new Result(ResultCode.Success, "添加成功")) :
                Json(new Result(ResultCode.Fail, "添加失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 游客信息 修改
        [HttpPost]
        public IHttpActionResult UpdateTouristInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            int TouristID = model.TouristID ?? 0;
            string TouristName = model.TouristName ?? "";
            string TouristPassword = model.TouristPassword ?? "";
            string TouristPhone = model.TouristPhone ?? "";
            string RegistrationDate = model.RegistrationDate ?? "";
            string Gender = model.Gender ?? "";
            string BirthDate = model.BirthDate ?? "";
            if (TouristID == 0)
            {
                return Json(new Result(ResultCode.Fail, "用户ID不能为空"));
            }
            if (string.IsNullOrEmpty(TouristName))
            {
                return Json(new Result(ResultCode.Fail, "用户名不能为空"));
            }
            if (string.IsNullOrEmpty(TouristPassword))
            {
                return Json(new Result(ResultCode.Fail, "密码不能为空"));
            }
            if (string.IsNullOrEmpty(TouristPhone))
            {
                return Json(new Result(ResultCode.Fail, "电话号码不能为空"));
            }
            if (string.IsNullOrEmpty(RegistrationDate))
            {
                return Json(new Result(ResultCode.Fail, "注册日期不能为空"));
            }
            if (string.IsNullOrEmpty(Gender))
            {
                return Json(new Result(ResultCode.Fail, "性别不能为空"));
            }
            if (string.IsNullOrEmpty(BirthDate))
            {
                return Json(new Result(ResultCode.Fail, "出生日期不能为空"));
            }
            try
            {
                var result = _TouristRegistrationDataBO.UpdateTouristInfo(model);
                return result ? Json(new Result(ResultCode.Success, "修改成功")) :
                Json(new Result(ResultCode.Fail, "修改失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 游客信息 删除
        [HttpPost]
        public IHttpActionResult DeleteTouristInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            int TouristID = model.TouristID ?? 0;
            if (TouristID == 0)
            {
                return Json(new Result(ResultCode.Fail, "ID 不能为空"));
            }
            try
            {
                var result = _TouristRegistrationDataBO.DeleteTouristInfo(TouristID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
        #region 用户信息 分页列表
        [HttpPost]
        public IHttpActionResult PageTouristList([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                int pageIndex = model.page_index ?? 0;
                int pageSize = model.page_size ?? 0;
                if (pageIndex < 1 || pageSize < 1) return Json(new Result(ResultCode.Fail,
                "参数错误"));
                var wc = _PageTouristListWhereC(model.condidate);
                var result = _TouristRegistrationDataBO.PageTouristList(pageIndex, pageSize, wc);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        private string _PageTouristListWhereC(dynamic qc)
        {
            var wc = string.Empty;
            if (qc == null) return wc;
            string tmp = "";
            tmp = ConvertHelper.DynamicToString(qc.TouristName);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" TouristName like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.TouristPassword);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" TouristPassword like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.TouristPhone);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" TouristPhone like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.RegistrationDate);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" RegistrationDate like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.Gender);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" Gender like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.BirthDate);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" BirthDate like '%{tmp}%'"; }
            return wc;
        }
        #endregion
    }
}
